import Vue from "vue";
import Vuex from "vuex";
import moduleA from "./moduleA.js";

Vue.use(Vuex);
import http from "@/utils/api/request.js";
import vuexPersist from "vuex-persist";

export default new Vuex.Store({
  state: {
    token: "",
    list: [],
  },
  mutations: {
    setlist(state, val) {
      if (JSON.parse(localStorage.getItem("vuex")).list) {
        state.list = JSON.parse(localStorage.getItem("vuex")).list;
      } else {
        state.list = val;
      }
    },
    add(state, val) {
      state.list.push({ title: val });
    },
  },
  actions: {
    getlist(context) {
      http("/data.json", "get").then((res) => {
        context.commit("setlist", res.data.data);
      });
    },
  },
  // action的参数不再是state了 而是context 代表vue实例
  modules: {
    moduleA,
  },
  getters: {},
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});
